package com.yamaikeji.plan.controller;

import com.yamaikeji.common.annotation.Log;
import com.yamaikeji.common.core.controller.BaseController;
import com.yamaikeji.common.core.domain.AjaxResult;
import com.yamaikeji.common.utils.security.AesUtils;
import com.yamaikeji.system.domain.tts.Activation;
import com.yamaikeji.system.domain.tts.Config;
import com.yamaikeji.system.service.tts.IActivationService;
import com.yamaikeji.system.service.tts.IConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 桑伟杰
 * @date 2025-04-08
 * @description 配置Controller
 */
@RestController
@RequestMapping("/api/tts/config")
public class ConfigController extends BaseController {

    @Autowired
    private IConfigService configService;

    @Autowired
    private IActivationService activationService;

    private static final String KEY = "KC4nOIMjuSk4/LLMxEeYT/7kqlAgSZy6fztHgg1St34=";
    private static final String IV = "6ToE5Fdy2U4S9yjFziMxig==";

    /**
     * 查询配置
     */
    @Log(title = "查询配置")
    @PostMapping("/getConfig")
    public AjaxResult getConfig(@RequestBody Activation activation) {
        activation = activationService.selectActivation(activation);
        if (activation == null) {
            return AjaxResult.error("激活码不存在");
        }
        Config config = new Config();
        config.setId(activation.getConfigId());
        config = configService.selectConfig(config);
        Config resConfig = new Config();
        resConfig.setRegion(config.getRegion());
        // 加密
        String encryptKey = AesUtils.encrypt(config.getKey(), KEY, IV);
        resConfig.setKey(encryptKey);
        return AjaxResult.success(resConfig);
    }
}
